home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / modlib / src / $name.P < prev    next >
Encoding:
Text File  |  1992-05-27  |  5.6 KB  |  197 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /****************************************************************************
  26.  *                                                                          *
  27.  * This file has been changed by to include Modules Extensions              *
  28.  * Changes by : Brian Paxton 1991/92                                        *
  29.  * Last update : June 1992                                                  *
  30.  *                                                                          *
  31.  * Organisation : University of Edinburgh.                                  *
  32.  * For : Departments of Computer Science and Artificial Intelligence        * 
  33.  *       Fourth Year Project.                                               *
  34.  *                                                                          *
  35.  ****************************************************************************/
  36.  
  37. /*  $name.P  */
  38.  
  39. $name_export([$name/2,$name0/2,$name/3]).
  40.  
  41. $name(X,L) :-
  42.     $name(X,L,perv).
  43.  
  44. % $name/3 performs the same task as the original, but checks for 'fun X = Y'
  45. % declarations and processes them properly.
  46.  
  47. $name(X, L,Str) :-
  48.      ( $isa_structuretag(Str) ->
  49.                (nonvar(X) -> $name_0(X,L,Str) ; $name_1(X,L,Str)) ;
  50.            ($writename('*** Error : Third argument to name/3 must be a structure tag'),
  51.             $nl,fail) ).
  52.  
  53. $name_0(X, L,_) :- integer(X), !, $b_intname(X,L,[]).
  54. $name_0(X, L,_) :- real(X), !, (X =:= 0 -> L = "0.0" ; $b_realname(X,L,[])).
  55. $name_0(X, L,_) :- nonvar(L), $name_numlist(L, X), !.
  56. $name_0(X, L,Str) :- 
  57.     ( var(L) ; Str == perv ), !,
  58.     ( ( $symtype($mapped_function(_,_,_,_),Type),
  59.         Type > 0,
  60.         $mapped_function(X0,0,X,Str) ) ;
  61.       X0 = X ), !,
  62.     $name0(X0,Full),
  63.     ( $append(_,[0'_,0'_|L],Full) ;
  64.       L = Full ), !.
  65. $name_0(X, L,Str) :- 
  66.     nonvar(L),
  67.     $name_1(Text,L,Str), !,
  68.     Text == X.
  69.  
  70. $name_1(_, L,_) :- var(L),!,fail.
  71. $name_1(X, L,_) :- nonvar(L), $name_numlist(L, X), !.
  72. $name_1(X, L,perv) :- $name_2(X,L), !.
  73. $name_1(X, L,Tag) :-
  74.     $name_2(Isperv,L),
  75.     ( $pervasive0(Isperv) ->
  76.           X = Isperv ;
  77.           ( $name_0(Tag,Tag0,perv),
  78.             $append(Tag0, [0'_,0'_|L], Chars),
  79.         $name_2(X0, Chars),
  80.         ( ( $symtype($mapped_function(_,_,_,_),Type),
  81.             Type > 0,
  82.             $mapped_function(X0,0,X,_) ) ;
  83.           X0 = X ) ) ), !.
  84.  
  85. $name_2(X,L) :- '_$builtin'(98). /* bldatom */
  86.  
  87. $name0(X,L) :- '_$builtin'(99).
  88.  
  89. $b_intname(N,F,L) :- 
  90.      (N < 0 ->
  91.           (N1 is -N,
  92.             F = [45|F1]        /* 45 == 0'- */
  93.       ) ;
  94.       (N1 = N, F1 = F)
  95.      ),
  96.      $b_intname_p(N1,F1,L).
  97.  
  98.  
  99. $b_intname_p(N,F,L) :-
  100.      N < 10,
  101.      D is N+48,
  102.      F=[D|L].
  103. $b_intname_p(N,F,L) :-
  104.      N>=10,
  105.      M is N//10,
  106.      D is N-M*10+48,
  107.      $b_intname_p(M,F,[D|L]).
  108.  
  109. $b_realname(X,F,L) :-
  110.      X < 0,
  111.      !,
  112.      Xp is -X,
  113.      F = [45|F1],
  114.      $b_realname(Xp,F1,L).
  115. $b_realname(X,F,L) :-
  116.     X =:= 0 -> F = L ;
  117.         ($floor(X,I),
  118.          $b_intname(I,F,[46|L1]),
  119.          F0 is X - I,
  120.          $b_mkint(F0,F1,L1,L2,7),
  121.          $b_intname(F1,FracName,[]),
  122.          $strip_trailing_zeros(FracName,L2,L)
  123.         ).
  124.  
  125. $b_mkint(X,Y,L1,L2,N) :-
  126.      N =:= 0 ->
  127.         (X1 is (X+0.5)/10,    /* lop off last digit, not useful */
  128.      $floor(X1,Y),
  129.      L1 = L2
  130.     ) ;
  131.     (N > 0,
  132.      ( ($floor(X,Y), X =:= Y) ->
  133.          L1 = L2 ;
  134.          (Z is 10 * X, $floor(Z,Z1),
  135.           (Z1 > 0 ->
  136.              L1 = L3 ;
  137.          L1 = [0'0|L3]
  138.           ),
  139.           N1 is N - 1, $b_mkint(Z,Y,L3,L2,N1)
  140.          )
  141.       )
  142.     ).
  143.  
  144. $strip_trailing_zeros([C|Rest],[C|SRest],L) :-
  145.      $strip_trailing_zeros(Rest,SRest,L,_).
  146.  
  147. $strip_trailing_zeros([],L,L,1).
  148. $strip_trailing_zeros([C|Rest], Out, Tl, Flag) :-
  149.      C =\= 0'0 ->
  150.           (Out = [C|ORest], Flag = 0,
  151.            $strip_trailing_zeros(Rest,ORest,Tl,_)
  152.       ) ;
  153.       ($strip_trailing_zeros(Rest,OutRest,Tl,Flag1),
  154.            (Flag1 =:= 1 ->
  155.                (Out = OutRest, Flag = 1) ;
  156.            (Out = [0'0|OutRest], Flag = 0)
  157.            )
  158.           ).
  159.  
  160. $name_numlist([C|CRest], N) :-
  161.      (nonvar(C), C =:= 45) ->
  162.           ($name_numlist_1(CRest, 0, N1),
  163.            N is -N1
  164.       ) ;
  165.       (C >= 48, C =< 57, 
  166.            M is C - 48,
  167.            $name_numlist_1(CRest,M,N)
  168.       ).
  169.  
  170. $name_numlist_1(L, M, N) :-
  171.      nonvar(L),
  172.      $name_numlist_2(L, M, N).
  173.  
  174. $name_numlist_2([], N, N).
  175. $name_numlist_2([C|CRest], M, N) :-
  176.      nonvar(C),
  177.      ((C >= 48, C =< 57) ->
  178.           (M1 is 10 * M + C - 48,
  179.            $name_numlist_1(CRest, M1, N)
  180.       ) ;
  181.       (C =:= 46 ->
  182.            (nonvar(CRest), $name_numlist_3(CRest,10,M,N))
  183.       )
  184.      ).
  185.  
  186. $name_numlist_3([], _, N, N).
  187. $name_numlist_3([C|Rest], Div, SoFar, N) :-
  188.      nonvar(C),
  189.      C >= 48, C =< 57,
  190.      Next is SoFar + (C-48)/Div,
  191.      Div1 is Div*10,
  192.      nonvar(Rest),
  193.      $name_numlist_3(Rest,Div1,Next,N).
  194.  
  195.  
  196.  
  197.